package com.web.imp;

import org.testng.annotations.AfterTest;

import com.app.AppContext;
import com.web.model.BrowserFactory;
import com.web.model.WebBrowser;

public abstract class AbstractWebTest extends AbstractTest{

	protected static final BrowserFactory browserFactory = AppContext.getBean(BrowserFactory.class);
	
	private static WebBrowser browser;
	
	public WebBrowser newBrowser(){
		
		if(browser == null || !browser.isAlive()){
		
			browser = browserFactory.newBrowser();
		}
		else
			browser.deleteCookies();
		
		return browser;
	}
	
	public boolean isThereBrowser(){
		return browser != null && browser.isAlive();
	}
	
	public WebBrowser getBrowser(){
		
		if (isThereBrowser()){
			
			return browser;
		}
		else
			throw new IllegalStateException("There is no browser");
	}
	
	protected void sleep(int segs){
		
		try {
			Thread.sleep(segs * 1000);
		
		} catch (InterruptedException e) {
		
			throw new IllegalStateException(e);
		}
	}
	
	@AfterTest
	public void afterClass(){
		
		if (isThereBrowser()){
			browser.quit();
		}
	}
}
